#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import wave as we
import numpy as np
import matplotlib.pyplot as plt

def wavread(path, dtype, ch_cnt):
    wavfile =  we.open(path,"rb")
    params = wavfile.getparams()
    framesra,frameswav= params[2],params[3]
    datawav = wavfile.readframes(frameswav)
    wavfile.close()
    datause = np.frombuffer(datawav, dtype=dtype)
    datause.shape = -1,ch_cnt
    datause = datause.T
    time = np.arange(0, frameswav) * (1.0/framesra)
    return datause,time

def main():
    #path = input("The Path is:")
    path = "ok_ch1_8.wav"    # 声音文件
    ch_cnt = 1                # 通道数
    dtype = np.uint8          # 量化位数, 一般是uint8, uint16, uint24较少  
    
    wavdata,wavtime = wavread(path, dtype, ch_cnt)
    plt.title("Night.wav's Frames")
    plt.subplot(211)
    plt.plot(wavtime, wavdata[0],color='green')
    if(ch_cnt >= 2):
        plt.subplot(212)
        plt.plot(wavtime, wavdata[1])
    plt.show()
    

if __name__ == "__main__":
    main()
